package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.search.query.SearchQueryObject;
import cn.wolfcode.luowowo.search.service.*;
import cn.wolfcode.luowowo.search.template.DestinationTemplate;
import cn.wolfcode.luowowo.search.template.StrategyTemplate;
import cn.wolfcode.luowowo.search.template.TravelTemplate;
import cn.wolfcode.luowowo.search.template.UserInfoTemplate;
import cn.wolfcode.luowowo.search.vo.SearchResult;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

/**
 * 主页控制
 */
@Controller
public class SearchController {

    @Reference
    //目的地查询服务
    private IDestinationSearchService destinationSearchService;
    @Reference
    private IUserInfoSearchService userInfoSearchService;
    @Reference
    private ITravelSearchService travelSearchService;
    @Reference
    private IStrategySearchService strategySearchService;
    @Reference
    private ISearchService searchService;

    @RequestMapping("/q")
    //访问主页,跳转
    public String index(@ModelAttribute("qo") SearchQueryObject qo, Model model) {
        if (qo.getKeyword() == null) {
            qo.setKeyword("");
        }
        String viewName = null;
        switch (qo.getType()) {
            //目的地
            case SearchQueryObject.SEARCH_DEST:
                //调用方法
                searchByDest(qo, model);
                viewName = "searchDest";
                break;
            //攻略
            case SearchQueryObject.SEARCH_STRATEGY:
                //调用方法
                searchByStrategy(qo, model);
                viewName = "searchStrategy";
                break;
            //游记
            case SearchQueryObject.SEARCH_TRAVEL:
                searchByTravel(qo, model);
                viewName = "searchTravel";
                break;
            //用户
            case SearchQueryObject.SEARCH_USER:
                searchByUserInfo(qo, model);
                viewName = "searchUser";
                break;
            //所有
            default:
                searchAll(qo, model);
                viewName = "searchAll";
        }
        return "/index/" + viewName;
    }

    private void searchAll(SearchQueryObject qo, Model model) {
        int total = 0;
        //用户信息
        List<UserInfoTemplate> uts = searchService.hightLightSearch(UserInfoTemplate.INDEX_TYPE_NAME,
                UserInfoTemplate.class, qo, "nickname", "city").getContent();
        total += uts.size();
        //游记信息
        List<TravelTemplate> tts = searchService.hightLightSearch(TravelTemplate.INDEX_TYPE_NAME,
                TravelTemplate.class, qo, "title", "summary").getContent();
        total += tts.size();
        //攻略信息
        List<StrategyTemplate> sts = searchService.hightLightSearch(StrategyTemplate.INDEX_TYPE_NAME,
                StrategyTemplate.class, qo, "title", "subTitle", "summary").getContent();
        total += sts.size();
        //目的地信息
        List<DestinationTemplate> dests = searchService.hightLightSearch(DestinationTemplate.INDEX_TYPE_NAME,
                DestinationTemplate.class, qo, "name", "info").getContent();
        total += dests.size();

        SearchResult result = new SearchResult();
        result.setUsers(uts);
        result.setTravels(tts);
        result.setStrategys(sts);
        result.setDests(dests);
        result.setTotal(total);
        model.addAttribute("data", result);
    }

    private void searchByUserInfo(SearchQueryObject qo, Model model) {
        //查询+高亮
        Page<UserInfoTemplate> userInfoTemplates = searchService.hightLightSearch(UserInfoTemplate.INDEX_TYPE_NAME,
                UserInfoTemplate.class, qo, "nickname", "city");
        model.addAttribute("page", userInfoTemplates);
    }

    private void searchByTravel(SearchQueryObject qo, Model model) {
        //查询+高亮
        Page<TravelTemplate> travelTemplates = searchService.hightLightSearch(TravelTemplate.INDEX_TYPE_NAME,
                TravelTemplate.class, qo, "title", "summary");
        model.addAttribute("page", travelTemplates);
    }

    private void searchByStrategy(SearchQueryObject qo, Model model) {
        //查询+高亮
        Page<StrategyTemplate> strategyTemplates = searchService.hightLightSearch(StrategyTemplate.INDEX_TYPE_NAME,
                StrategyTemplate.class, qo, "title", "subTitle", "summary");
        model.addAttribute("page", strategyTemplates);
    }

    private void searchByDest(SearchQueryObject qo, Model model) {
        if (qo.getKeyword() == null) {
            SearchResult result = new SearchResult();
            model.addAttribute("data", result);
            return;
        }
        //查询结果数
        int num = 0;
        //查询目的地
        DestinationTemplate destination = destinationSearchService.getByName(qo.getKeyword());
        //共享
        model.addAttribute("dest", destination);
        if (destination != null) {
            num = 1;
        }
        //查询对应的游记
        List<TravelTemplate> tts = travelSearchService.listByDestName(qo.getKeyword());
        num += tts.size();
        //查询对应的攻略
        List<StrategyTemplate> sts = strategySearchService.listByDestName(qo.getKeyword());
        num += sts.size();
        //查询对应的用户
        List<UserInfoTemplate> uts = userInfoSearchService.listByCity(qo.getKeyword());
        num += uts.size();

        //共享
        SearchResult result = new SearchResult();
        result.setTravels(tts);
        result.setStrategys(sts);
        result.setUsers(uts);
        result.setTotal(num);
        model.addAttribute("data", result);
    }

}
